<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="bean.User" %>
<%@ page import="bean.News" %>
<%@ page import="java.util.List" %>
<%@ page import="bean.NewsofUser" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>时讯界新闻发布系统</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet">
    <link href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css" rel="stylesheet">
    <style>
        body {
            background-color: #eaf6f6;
        }
        .container-fluid {
            display: flex;
            height: 100vh;
            padding: 0;
        }
        .sidebar {

            background-color: #333;
            /*background-color: #2aabd2;*/
            color: white;
            padding-top: 20px;
            flex: 0 0 200px; /* fixed width for sidebar */
            display: flex;
            flex-direction: column;
            align-items: center;
            height: 100vh;
        }
        .sidebar a {
            color: white;
            display: block;
            padding: 10px 15px;
            text-decoration: none;
            width: 100%;
            text-align: center;
        }
        .sidebar a:hover, .sidebar .active {
            /*background-color: #2aabd2;*/
            background-color: #575757;
            text-decoration: none;
        }
        .main-content {
            background-color: #eaf6f6;
            flex-grow: 1;
            padding: 30px;
            display: flex;
            flex-direction: column;
        }
        .user-info {
            text-align: center;
            padding: 20px 0;
        }
        .user-info img {
            width: 60px;
            border-radius: 50%;
        }
        .user-info p {
            margin: 10px 0 0;
        }
        .editor-container {
            flex-grow: 1;
            display: flex;
            flex-direction: column;
        }
        #editor-text-area {
            flex-grow: 1;
            height: auto;
        }
        .table-container {
            margin-bottom: 20px;
        }
        .center-container {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<% User user = (User) request.getSession().getAttribute("user");

    User userupdate = (User) request.getSession().getAttribute("userupdate");
    if (userupdate != null) {
        // 将 news 对象设置为 request 属性，以便在页面中使用
        request.setAttribute("userupdate", userupdate);
//        request.getSession().setAttribute("newid", news.getId());
        // 从 session 中移除 news 对象
        request.getSession().removeAttribute("userupdate");
    }
    Integer start = (Integer) request.getSession().getAttribute("start");
    //System.out.println(start + "qqq");

%>
<div class="container-fluid">

    <div class="col-md-10 main-content">
        <div class="mb-3">
            <button class="btn btn-primary" onclick="redirectToPage()">添加用户</button>
            <form class="d-inline-block w-auto ml-2" action="../userMan.do" method="post">
                <input type="text" class="form-control d-inline-block w-auto ml-2" name="select" placeholder="查询用户">
                <input class="btn btn-secondary ml-2" type="submit" value="查询" name="submit"/>
            </form>
        </div>
        <div class="table-container" style="width: 1230px;">
            <table class="table table-bordered" >
                <thead>
                <tr>
                    <th>ID</th>
                    <th>用户头像</th>
                    <th>账号</th>
                    <th>性别</th>
                    <th>身份</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:if test="${startuser == 0}">
                    <tr>
                        <td>1(样例)</td>
                        <td>
                            <img src="../resources/headimg.png" alt="头像" width="20" height="20">
                        </td>
                        <td>账号</td>
                        <td>男</td>
                        <td>普通用户</td>
                        <td>
                            <a class="btn btn-warning btn-sm" href="#" >修改</a> &nbsp;
                            <a class="btn btn-danger btn-sm" href="#" onclick="return confirm('是否删除？')">删除</a>
                        </td>
                    </tr>
                </c:if>
                <c:forEach var="u" items="${userselectlist}">
                    <tr>
                        <td>${u.id }</td>
                        <td>
<%--                            <img src="D:\javacode\press-release-system\web\resources\headimg\${ u.headimg}" alt="用户头像" width="50" height="50">--%>
                            <img src="../resources/headimg/${ u.headimg}" alt="用户头像" width="50" height="50">
                        </td>
                        <td>${u.username }</td>
                        <c:if test="${u.sex == '0'}">
                            <td>男</td>
                        </c:if>
                        <c:if test="${u.sex == '1'}">
                            <td>女</td>
                        </c:if>
                        <c:if test="${u.sex == '2'}">
                            <td>未知</td>
                        </c:if>
                        <c:if test="${u.note == '0'}">
                            <td>管理员</td>
                        </c:if>
                        <c:if test="${u.note == '1'}">
                            <td>普通用户</td>
                        </c:if>
<%--                        <td>${u.note }</td>--%>
                        <td>
                            <a class="btn btn-warning btn-sm" href="../userMan.do?updateid=${u.id }" >修改</a> &nbsp;
                            <a class="btn btn-danger btn-sm" href="../userMan.do?deleteid=${u.id }" onclick="return confirm('是否删除？')">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            <div class="center-container">
                <span class="center-text">${pageUser}</span>
            </div>
        </div>
    </div>
</div>
<!-- Account Management Modal -->
<div class="modal fade" id="accountModal" tabindex="-1" aria-labelledby="accountModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="accountModalLabel">账号管理</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="../userServlet?id=<%= user != null ? user.getId() : "" %> " method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="avatar">当前头像</label>
                        <div>
                            <img src="../resources/headimg/<%= user != null ? user.getHeadimg() : "" %>" alt="当前头像" style="max-width: 150px; max-height: 150px;">
                        </div>
                        <label for="avatar">修改头像</label>
                        <input type="file" class="form-control-file" id="avatar" name="avatar">
                    </div>
                    <div class="form-group">
                        <label for="username">修改账号</label>
                        <input type="text" class="form-control" id="username" name="username" placeholder="请输入新的账号">
                    </div>
                    <div class="form-group">
                        <label for="password">修改密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="请输入新的密码">
                    </div>
                    <div class="form-group">
                        <label for="gender">修改性别</label>
                        <select class="form-control" id="gender" name="sex">
                                <option value="0" <%= user != null && "0".equals(String.valueOf(user.getSex())) ? "selected" : "" %>>男</option>
                                <option value="1" <%= user != null && "1".equals(String.valueOf(user.getSex())) ? "selected" : "" %>>女</option>
                                <option value="2" <%= user != null && "2".equals(String.valueOf(user.getSex())) ? "selected" : "" %>>其他</option>
                        </select>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button type="submit" class="btn btn-primary" name="update1" value="update1">保存修改</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- Add User Modal -->
<div class="modal fade" id="addUserModal" tabindex="-1" aria-labelledby="addUserModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addUserModalLabel">添加用户</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form action="../userServlet" method="post" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="avatar">头像</label>
                        <input type="file" class="form-control-file" id="avataru" name="avataru">
                    </div>
                    <div class="form-group">
                        <label for="username">账号</label>
                        <input type="text" class="form-control" id="usernameu" name="usernameu" placeholder="请输入账号">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="passwordu" name="passwordu" placeholder="请输入密码">
                    </div>
                    <div class="form-group">
                        <label for="gender">性别</label>
                        <select class="form-control" id="genderu" name="sex">
                            <option value="0">男</option>
                            <option value="1">女</option>
                            <option value="2">其他</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="role">角色</label>
                        <select class="form-control" id="roleu" name="role">
                            <option value="0">管理员</option>
                            <option value="1">普通用户</option>
                        </select>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                        <button type="submit" class="btn btn-primary" name="add" value="add">添加用户</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<div class="container">
    <!-- Your existing page content -->

    <!-- Add User Modal -->
    <div class="modal fade" id="addUserModal1" tabindex="-1" aria-labelledby="addUserModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="addUserModalLabel1">修改用户</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="../userServlet?id=<%= userupdate != null ? userupdate.getId() : "" %> " method="post" enctype="multipart/form-data">
                        <div class="form-group">
                            <label for="avatar">当前头像</label>
                            <div>
                                <img src="../resources/headimg/<%= userupdate != null ? userupdate.getHeadimg() : "" %>" alt="当前头像" style="max-width: 150px; max-height: 150px;">
                            </div>
                            <label for="avatar">修改头像</label>
                                <input type="file" class="form-control-file" id="avataruu"  name="avataruu">
<%--                            <input type="file" class="form-control-file" id="avataruu"  name="avataruu">--%>
                        </div>
                        <div class="form-group">
                            <label for="username">账号</label>
                            <input type="text" class="form-control" id="usernameuu" name="usernameuu" placeholder="请输入账号" value="<%= userupdate != null ? userupdate.getUsername() : "" %>">
                        </div>
                        <div class="form-group">
                            <label for="password">密码</label>
                            <input type="password" class="form-control" id="passworduu" name="passworduu" placeholder="请输入密码" value="<%= userupdate != null ? userupdate.getPassword() : "" %>">
                        </div>
                        <div class="form-group">
                            <label for="gender">性别</label>
                            <select class="form-control" id="genderuu" name="sex">
                                <option value="0" <%= userupdate != null && "0".equals(String.valueOf(userupdate.getSex())) ? "selected" : "" %>>男</option>
                                <option value="1" <%= userupdate != null && "1".equals(String.valueOf(userupdate.getSex())) ? "selected" : "" %>>女</option>
                                <option value="2" <%= userupdate != null && "2".equals(String.valueOf(userupdate.getSex())) ? "selected" : "" %>>其他</option>
                            </select>

                        </div>
                        <div class="form-group">
                            <label for="role">角色</label>
                            <select class="form-control" id="role" name="role">
                                <option value="0" <%= userupdate != null && userupdate.getNote().equals("0")  ? "selected" : "" %>>管理员</option>
                                <option value="1" <%= userupdate != null && userupdate.getNote().equals("1")  ? "selected" : "" %>>普通用户</option>
                            </select>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                            <button type="submit" class="btn btn-primary" name="update2" value="update2">修改用户</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>

<<script>
    function redirectToPage() {
        $('#addUserModal').modal('show');
    }
</script>
<script>
    $(document).ready(function() {
        <% if (userupdate != null) { %>
        $('#addUserModal1').modal('show');
        <% } %>
    });
</script>
</body>
</html>
